package web;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import persistence.entity.Customer;
import persistence.entity.Account;
import persistence.entity.Accounttransaction;
import session.stateless.*;

/**
 *
 * @author Craig
 */
@ManagedBean
@SessionScoped
public class CustomerManagedBean {
    @EJB
    private CustomerFacadeRemote customerFacade;
    
    @EJB
    private AccountFacadeRemote accountFacade;
    
    @EJB
    private BillerFacadeRemote billerFacade;
    
    @EJB
    private AccountHelperRemote accountHelper;
    
    @EJB
    private AccountTransactionHelperRemote accountTransactionHelper;

    private int custID;
    private String custName;
    
    private String custPass;
    private String custAddress;
    private String custPhone;
    private List<Account> accounts;
    private Account transactionAccount;
    private Account transactionToAccount;
    private Integer transactionAmount;
    private String transactionDescription;
    private Integer payBillerID;
    private List<Accounttransaction> accounttransactions;

    public Integer getPayBillerID() {
        return payBillerID;
    }

    public void setPayBillerID(Integer payBillerID) {
        this.payBillerID = payBillerID;
    }

    public String getTransactionDescription() {
        return transactionDescription;
    }

    public void setTransactionDescription(String transactionDescription) {
        this.transactionDescription = transactionDescription;
    }

    public Integer getTransactionAmount() {
        return transactionAmount;
    }

    public void setTransactionAmount(Integer transactionAmount) {
        this.transactionAmount = transactionAmount;
    }

    public Account getTransactionToAccount() {
        return transactionToAccount;
    }

    public void setTransactionToAccount(Account transactionToAccount) {
        this.transactionToAccount = transactionToAccount;
    }

    public List<Accounttransaction> getAccounttransactions() {
        return accounttransactions;
    }

    public void setAccounttransactions(List<Accounttransaction> accounttransactions) {
        this.accounttransactions = accounttransactions;
    }

    public Account getTransactionAccount() {
        return transactionAccount;
    }

    public void setTransactionAccount(Account transactionAccount) {
        this.transactionAccount = transactionAccount;
    }

    public List<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public int getCustID() {
        return custID;
    }

    public void setCustID(int custID) {
        this.custID = custID;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustPass() {
        return custPass;
    }

    public void setCustPass(String custPass) {
        this.custPass = custPass;
    }

    public String getCustPhone() {
        return custPhone;
    }

    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    
    /**
     * Creates a new instance of CustomerManagedBean
     */
    public CustomerManagedBean() {
        custID = 0;
        custName = "";
        
        custPass = "";
        custAddress = "";
        custPhone = "";
        accounts = new ArrayList<Account>();
        transactionAccount = null;
        transactionToAccount = null;
        transactionAmount = 0;
        transactionDescription = "";
        payBillerID = 0;
        accounttransactions = new ArrayList<Accounttransaction>();
    }
    
    public String login() {
        Boolean authentic = customerFacade.authenticate(custID, custPass, true);
        if (authentic) {
            Customer c = customerFacade.find(custID);
            custName = c.getCustName();
            custAddress = c.getCustAddress();
            custPhone = c.getCustPhone();
            accounts = accountFacade.getAccountList(custID, true);
            if (accounts.size() > 0) {
                transactionAccount = accounts.get(0);
                transactionToAccount = accounts.get(0);
            }
            return "success";
        } else {
            return "failure";
        }
    }
    
    public String update() {
        Customer c = customerFacade.find(custID);
        c.setCustAddress(custAddress);
        c.setCustPhone(custPhone);
        if (custPass.equals("")) {
            custPass = c.getCustPassword();
        } else {
            c.setCustPassword(custPass);
        }
        customerFacade.edit(c);
        return "success";
    }
    
    public String viewTransactions() {
        if (transactionAccount == null) {
            return "failure";
        } else {
            accounttransactions = accountTransactionHelper.getTransactionsForAccount(transactionAccount);
            System.out.println(accounttransactions);
            return "success";
        }
    }
    
    public String transfer() {
        boolean fromAccNotNull = transactionAccount != null;
        boolean toAccNotNull = transactionToAccount != null;
        boolean amountIsValid = transactionAmount != null && transactionAmount > 0;
        
        boolean validTransaction = fromAccNotNull && toAccNotNull && amountIsValid;
        
        if (validTransaction) {
            Accounttransaction at = accountTransactionHelper.createTransfer(
                    transactionAmount, transactionDescription,
                    transactionAccount.getAccountId(), transactionToAccount.getAccountId());
            transactionAmount = 0;
            transactionDescription = "";
            if (at != null) {
                accounts = accountFacade.getAccountList(custID, true);
                if (accounts.size() > 0) {
                    transactionAccount = accounts.get(0);
                    transactionToAccount = accounts.get(0);
                }
                return "success";
            }
        }
        return "failure";
    }
    
    public String payBill() {
        boolean fromAccNotNull = transactionAccount != null;
        boolean billerIDValid = payBillerID != null && payBillerID > 0 &&
                billerFacade.isValidBillerID(payBillerID);
        boolean amountIsValid = transactionAmount != null && transactionAmount > 0;
        
        boolean validTransaction = fromAccNotNull && billerIDValid && amountIsValid;
        if (validTransaction) {
            Accounttransaction at = accountTransactionHelper.createBillPayment(
                    transactionAmount, transactionDescription,
                    transactionAccount.getAccountId(), payBillerID);
            payBillerID = 0;
            transactionAmount = 0;
            transactionDescription = "";
            if (at != null) {
                accounts = accountFacade.getAccountList(custID, true);
                if (accounts.size() > 0) {
                    transactionAccount = accounts.get(0);
                    transactionToAccount = accounts.get(0);
                }
                return "success";
            }
        }
        payBillerID = 0;
        transactionAmount = 0;
        transactionDescription = "";
        return "failure";
    }
}
